#ifndef GITLNONBLOCKEVTDISPATCHER_H
#define GITLNONBLOCKEVTDISPATCHER_H

#include <QThreadPool>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QList>
#include "gitlevent.h"
#include "gitliomsg.h"
#include "gitlmodual.h"
#include "gitldef.h"

class GitlNonBlockEvtHandlerThread : public QThread
{
public:
    GitlNonBlockEvtHandlerThread(QList<GitlModual*>* pcModuals)
    {
        m_pcModuals = pcModuals;
    }

protected:
    void run()
    {
        GitlEvent* pcEvt = &m_cEvt;
        /// notify moduals
        for(int iModualCount = 0; iModualCount < m_pcModuals->size(); iModualCount++ )
        {
            if( pcEvt->getEvtName() == "*" || m_pcModuals->at(iModualCount)->isListenToEvt(pcEvt->getEvtName()) )
            {
                GitlModual* pModual = m_pcModuals->at(iModualCount);

                sleep(10);
                if( pModual->detonate(pcEvt) )
                {
                    GitlIOMsg::getInstance()->msgOut(QString("Modual %1 Success in Processing %2 Event.").arg(pModual->getModualName()).arg(pcEvt->getEvtName()), GITL_MSG_DEBUG );
                }
                else
                {
                    GitlIOMsg::getInstance()->msgOut(QString("Modual %1 Fail in Processing %2 Event.").arg(pModual->getModualName()).arg(pcEvt->getEvtName()), GITL_MSG_WARNING );
                }
            }

        }

    }


    ADD_CLASS_FIELD( GitlEvent, cEvt, getEvt, setEvt )
    ADD_CLASS_FIELD_PRIVATE ( QList<GitlModual*>*, pcModuals)
};




class GitlNonBlockEvtDispatcher
{
public:
    GitlNonBlockEvtDispatcher(QList<GitlModual*>* pcModuals);
    ~GitlNonBlockEvtDispatcher();
    /*! non-blocking notification
      */
    void postEvent(GitlEvent* pcEvt);


    ADD_CLASS_FIELD( QThreadPool*, pcThreadPool, getThreadPool, setThreadPool)

    ADD_CLASS_FIELD_PRIVATE( QMutex, cEvtHandlerQueLocker )
    ADD_CLASS_FIELD_PRIVATE( QList<GitlNonBlockEvtHandlerThread*>, cNonBlockEvtHandlerThreads)

    ADD_CLASS_FIELD_PRIVATE( QList<GitlModual*>*, pcModuals )
};

#endif // GITLNONBLOCKEVTDISPATCHER_H
